Find a single character on a line just press F in normal mode followed by the characters you want to
find.
So to move to the character on the first line you will type f a then F A again to move to the next one
and the next one and the next one.
This is better than moving to a specific column numbered with the pipe character to search for a character.
Backwords use capital F followed by the character instead.
Capital F A
to search for a word in the document type a slash followed by the word
to go to the next thing sense of the foreign word press an to go to its previous instance.
Press capital N.
to highlight old found words.
What can said diea highlighted.
Search feature on.
By typing in Colon Set h l.
Search.
You can also set this in the VM RC file like we have done in the previous video to search for multiple
instances of the same word you can just place the cursor on the word and press asterisk.
Then continue pressing and Capitol and to move to the next or previous instance of the word in the document.
Regular expressions can be used when searching for text.
For example square brackets lets you specify a range of characters
like for the history command.
If we type slash and press the up arrow will get back to the last search made
this time.
It hasn't found anything as the character t is not in the range specified in the square brackets before
diving into regular expressions.
Let's search some words and replace them.
To do this we'll have to type in colon followed by s.
The word to search for in includes the main slashes.
Then the word we want to replace that is Lesch again.
We replaced the first 8 character in the current line with B to replace all the characters with B's
for all instances of the found character.
We'll then have to append the G character at the end of our command.
Let's reverse it.
We can define a range of lines.
We want our substitution to take place.
We all have to define the range in numbers before the replacement command
let's press you to undo the replacement we can alternatively enter in visual block mode.
Select the lines we want to process and do the same
press again if we want the replacement to take place globally.
That is every instance of the farm character or word throughout the document.
We will use the person sign rather than a numerical range
G matches and replaces each occurrence on the line.
The character ignores case sensitivity press you again to undo let me give you a taste of the power
of regular expressions in vim.
Let's see we wanted to remove all blank spaces at the beginning of each line.
Now try doing it by hand on a document containing more than 500 lines.
All right.
But what exactly do we do with the previous command.
Press colon then be up are OK with colon percent as we instruct them that we want to perform the replacement
on the entire document.
The exponential character MetroWest the beginning of the line as you might remember the dollar sign
instead goes to the end of the line as a movement key.
Are these also used in the regular expression syntax to backslash s mattress one blank space of any
kind.
If we removed that backslash which is an escape character then we would have matched the actual s character.
The asterisk matches zero or more instances of whatever character or range of characters is on its left.
Then there's Lesch and the Pirates are in too fine.
And in the end there's another slash that doesn't contain anything before it because we want to replace
the found pattern with nothing by the same token we called Ed a new line character at the end of every
line.
How would we do it.
Match the end of the line then replace the end with a carriage return character which is the escaped
our character
press you to run you.
Regular expressions are just an advanced search algorithm that uses special syntax to match and replace
text.
We can go very deep.
I'll give you the essentials so that you'll be able to figure things out on your own.
No problem.
First let's go to the morgue X dot com to get a reference of all the regular expressions escape characters
and more
as you can see to darts matches any character we see the backslash as we used earlier to match whitespace.
Then there's the one for digits and another one for words alphabetic characters and so on with other
escape characters that offer more granularity.
If we needed it then we have quantifiers.
The asterisk we have seen earlier the plus character then ranges we have character ranges to define
subsets of alphanumeric characters in square brackets.
Nothing special there.
Then in the end we have grouping and back references which are very useful.
But let's first break this a bit with quantifiers
let's match the e-mails in the document and remove them.
We search for 0 or more words than the at symbol.
Then again zero or more words the escape.
Dodge Alstad will look for any character.
Then again zero or more words then search for the end of the line and the new line character.
That's actually invisible but it's located after the end of the line.
Press enter.
We've removed the email addresses from the document.
The actual regular expression commination the search for email addresses is more complicated because
of all the other special characters that can be present in email addresses.
As you can see here there are ranges quantifiers special characters are considered but it really isn't
that much more complicated once you analyze it bit by bit lets do more with quantifiers.
Let's say we wanted to merge only the first two lines and replace the lines with one two or three.
The quantified in curly braces specifies we're looking for.
So you roll to maximum occurrences of the characterising the alphabet where instead the asterisks was
a shorthand for the zero or more occurrences
press.
You again went back reference's.
We want to substitute parts of the matched pattern while preserving a part of it.
Let me demonstrate.
Let's say we wanted to preserve the first three letters of each line starting from the start of the
line.
Let's first make sure that we're matching the characters correctly by replacing them with x x x from
the start of the line match and the alphabetic character up to three max characters.
Replace it with x x x
press Enter
OK worked.
Price you
back references we can replace the text with matched but also reference.
While we are substituting it in the same breath.
That's why they're called back references to make use of them.
We have to create a group by enclosing the pattern in parenthesis.
The parentheses also need to be escaped
this is already getting mesir doesn't it.
We can reference divides or in parentheses by typing in backslash one where one is the first group we
have defined.
If we had another group in parenthesis after the first one we will def back reference it with backslash
do and so on.
Coming up with regular expressions to solve a problem that would require back references can seem like
a daunting process or some kind of a puzzle that will require too much time.
But what if you need to modify hundreds or thousands of lines at the same time.
If the substitution desk to take place follows a specific pattern no matter how complex you'll be able
to use regular expressions to solve the problem at hand.
Let's say for example you wanted to replace all the hypertext worldlings of a web page to go nowhere.
That is replacing the H ref a tag attribute with a hash.
We have downloaded the page locally
we want to make sure that when we click on links they all read direct to the same page by replacing
all the links with our hash.
Let's replace all the links with only one regular expression.
Let's take a look at a common tag
where put a sheriff equals in a group because we want to back reference it play or
then we match any character within double quotes the minus in curly braces matches only the first instance
in the line in Lubbock quotes.
Else it will keep searching until it finds the very last double quote on the line and we don't want
that.
Then we substitute the matched text with the A H ref equals portion of the match.
The word my back referencing it.
Then add the ash in double quotes with G when instructed to do the same for every line in the document.
Now all links when clicked will not try to go anywhere
say with colon W..
Open a new terminal tab control 50
regular expressions can get complex quickly but they are very powerful.
No way in hell you will have done that by hand as efficiently.
There are many things that you one do by hand anymore.
Once you learn regular expressions start using every day and try to implement regular expressions in
your everyday activities.
You'll get the hang of them fast.
I assure you there is also the G command.
I often use it to remove blank lines.
Words are not needed.
It allows you to perform an operation based on the matched text
this command will remove all the empty lines in the document.
This other one will copy all the lines that contain the given pattern and put them to the end of the
file.
The other one moves them to the end of the file.
The do command searches for a string or regular expression then performs an action on the matched lines.
OK let's move on.